package cc.linker.steplink.crm.merchant.task;

import cc.linker.steplink.crm.commons.lock.RedisLock;
import cc.linker.steplink.crm.merchant.service.RefundService;
import cc.linker.steplink.crm.merchanth5.service.acs.ProtectRecordService;
import cc.linker.steplink.crm.merchanth5.service.acs.PurchaseProcessService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Slf4j
@Component
public class RefundTask {


    @Autowired
    private RefundService refundService;
    @Autowired
    private PurchaseProcessService purchaseProcessService;
    @Autowired
    private ProtectRecordService protectRecordService;

    /**
     * 定时修改超过七天时间的退款状态
     */
    @Async
    @RedisLock("RefundTask_changeRefundStatus")
    @Scheduled(cron = "40 */5 * * * ?")
    public void changeRefundStatus() {
        //获取订单符合要求的订单（123457）
        List<Map> overtimeList = refundService.getOvertimeList();
        if (overtimeList != null && overtimeList.size() > 0) {
            for (Map map : overtimeList) {
                refundService.updateStatusOvertime(map);
            }
        }
    }

    /**
     * 定时查看微信退款状态
     */
//    @Async
//    @RedisLock("RefundTask_refundQueryStatus")
//    @Scheduled(cron = "40 0*/1 * * ?")
    public void refundQueryStatus() {
        //获取订单符合要求的订单（5）
        List<Map<String, String>> refundQueryList = refundService.refundQueryList();
        if (refundQueryList != null && refundQueryList.size() > 0) {
            for (Map<String, String> map : refundQueryList) {
                String orderSn = map.get("orderSn");
                String outRefundNo = map.get("outRefundNo");
                String refundId = map.get("refundId");
                String status = purchaseProcessService.refundQuery(orderSn, outRefundNo, refundId);
                //退款成功（其他状态等待下期）
                if ("SUCCESS".equals(status)) {
                    String recId = map.get("recId");
                    if (recId == null) {
                        recId = "-1";
                    }
                    String orgId = map.get("orgId");
                    protectRecordService.overAction(orderSn, orgId, Long.valueOf(recId));
                }

            }
        }
    }


}
